home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c
- Subject: Re: Help: floating point/use of floor
- Date: 19 Jan 1996 17:00:52 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan19120052@g7240065.bridge.bst.bls.com>
- References: <4dogri$fta@gate.service.britgas.co.uk>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: andy.crutchlow@service.britgas.co.uk's message of 19 Jan 1996
- 16:28:34 GMT
-
- In article <4dogri$fta@gate.service.britgas.co.uk> andy.crutchlow@service.britgas.co.uk (Andy Crutchlow) writes:
-
- : Can anyone help.
-
- : Make the following display 75.99
- : main()
- : {
- : int i;
- : float f;
- : i = 7599;
- : f = i / 100.00;
- : printf("%f\n",f);
- : }
-
- The problem with floats (and doubles) is that some numbers are not
- directly representable.
- What may not be directly representable in floats may be representable in
- doubles.
- I can make it print 75.99 but I can't guarentee yours will do the same.
-
- #include <stdio.h>
-
- int
- main(void)
- {
- float f = 75.99;
- double d = 75.99;
- printf("%f\n", f); /* prints 75.989998 */
- printf("%.2f\n", f); /* prints 75.99 */
- printf("%g\n", f); /* prints 75.99 */
- printf("%e\n", f); /* prints 7.599000e+01 */
-
- printf("%f\n", d); /* prints 75.990000 */
- printf("%g\n", d); /* prints 75.99 */
- printf("%e\n", d); /* prints 7.599 */
- }
-
- : I believe the correct answer involves using function floor but I can't
- : remember how.
-
- floor() will not help - it returns the largest integer not greater than
- its argument.
-
- Regards
-
- -A.
- --
- | A.Champion |
-